import { getInformationList, dictTree } from "./request.js";
import { formatURL, setBrowseNumber } from "./base.js";
$(function () {
    let total = 0;
    if ($(document.body).width() < 1000) {
        $(".content .collapse").removeClass("show")
    }
    $("#header").load("./layout/header.html", function () {
        $($(".navbarList > li")[3]).addClass("headerActive");
        $($(".navbarList > li")[3]).find("a").attr({
            href: "javascript:;"
        });
    });

    $("#footer").load("./layout/footer.html", function () {
        $("#accordionExample").on("hide.bs.collapse show.bs.collapse", function (e) {
            let target = e.target;
            let partens = "#" + $("#" + target.id).attr("aria-labelledby");
            $(partens).find("i").toggleClass("bi-chevron-down");
            $(partens).find("i").toggleClass("bi-chevron-up");
        });

        setBrowseNumber();
    });
    $(".tabbar_m").on("click", ".tabbar", function () {
        $(".tabbar_m .tabbar").removeClass("active");
        $(this).addClass("active");
        $('.showBox').html($(this).text())
        $("#collapseTabbar").collapse('hide');
        getStickList({
            pageNum: 1,
            pageSize: 100,
            type: $(this).data("key"),
            isStick:1
        })
        toPage({
            pageNum: 1,
            pageSize: 4,
            type: $(this).data("key")
        })
    })
    function getStickList(params) {
        getInformationList(params)
            .then((res) => {
                if (res.data.records.length === 0) {
                    $('.stickList').hide();
                } else {
                    $('.stickList').show();
                }
                dataHtmlStickList(res.data.records);
            })
            .catch((err) => {
                console.log(err);
            });
    }
    const getList = (params) => {
        getInformationList(params)
            .then((res) => {
                dataHtmlNews(res.data.records);
            })
            .catch((err) => {
                console.log(err);
            });
    };
    const toPage = (params = []) => {
        getInformationList(params)
            .then((res) => {
                dataHtmlNews(res.data.records);
                total = res.data.total;
                layui.use(['laypage', 'layer'], function () {
                    var laypage = layui.laypage
                        , layer = layui.layer;
                    //总页数大于页码总数
                    laypage.render({
                        elem: 'demo1'
                        , count: total, //数据总数
                        limit: 4
                        , jump: function (obj, first) {
                            if (!first) {
                                getList({
                                    pageNum: obj.curr,
                                    pageSize: 4,
                                    type: params.type
                                })
                            }
                        }
                    });
                });
            })
            .catch((err) => {
                console.log(err);
            });
    };
    const getDictTree = () => {
        dictTree('INFORMATION_TYPE')
            .then((res) => {
                if (res.data.children.length > 0) {
                    let str = "";
                    let btnStr = res.data.children[0].label;
                    for (let index = 0; index < res.data.children.length; index++) {
                        const element = res.data.children[index];
                        if (location.search) {
                            str += `
                            <div class="tabbar ${decodeURI(location.search.split("&")[0].split("=")[1]) == element.label ? 'active' : index == 0 && location.search == null ? 'active' : ''}" data-type="${element.label}" data-key="${element.label + '/' + element.dictCode}">
                            ${element.label}
                            </div>`
                            if(decodeURI(location.search.split("&")[0].split("=")[1]) == element.label){
                                btnStr = element.label;
                            }
                        } else {
                            str += `
                            <div class="tabbar ${index == 0 ? 'active' : ''}" data-type="${element.label}" data-key="${element.label + '/' + element.dictCode}">
                            ${element.label}
                            </div>`
                        }

                    }
                    $(".tabbar_m").html(str);
                    $('.showBox').html(btnStr)
                    getStickList({
                        pageNum: 1,
                        pageSize: 100,
                        type: location.search ? decodeURI(location.search.split("&")[1].split("=")[1]) : (res.data.children[0].label + '/' + res.data.children[0].dictCode)
                    })
                    toPage({
                        pageNum: 1,
                        pageSize: 4,
                        type: location.search ? decodeURI(location.search.split("&")[1].split("=")[1]) : (res.data.children[0].label + '/' + res.data.children[0].dictCode)
                    })
                }
            })
            .catch((err) => {
                console.log(err);
            });
    };
    getDictTree();

    function dataHtmlNews(params) {
        let str = "";
        let type = encodeURI($('.tabbar_m .active').data("key"));
        for (let index = 0; index < params.length; index++) {
            const ele = params[index];
            str += ` <a class="article" href="./dynamicInfo.html?id=${ele.id}&type=${type}">
                        <img src="${formatURL(ele.titleImg)}" alt="">
                        <div class="article_c">
                            <h3>${ele.title}</h3>
                            <div class="time">${ele.createTime}</div>
                            <p>${ele.sketch}</p>
                        </div>
                    </a>`
        }
        $(".newsList .list").html(str)
    }

    function dataHtmlStickList(params) {
        let str = "";
        let type = encodeURI($('.tabbar_m .active').data("type"));
        if (params.length > 0) {
            str += ` <a class="article" href="./dynamicInfo.html?id=${params[0].id}&type=${type}">
            <img src="${formatURL(params[0].titleImg)}" alt="">
            <div class="article_c">
                <h3>${params[0].title}</h3>
                <div class="time">${params[0].createTime}</div>
                <p>${params[0].sketch}</p>
            </div>
        </a>`
        }
        for (let index = 1; index < params.length; index++) {
            const ele = params[index];
            str += ` <a class="article" href="./dynamicInfo.html?id=${ele.id}&type=${type}">
                        <div class="article_c">
                            <h3><span class="serial">${index}</span>${ele.title}</h3>
                            <div class="time">${ele.createTime}</div>
                        </div>
                    </a>`
        }
        $(".stickList .list").html(str)
    }
})